package com.bo.service;

import com.alibaba.fastjson.JSONObject;
import com.bo.dto.CarHornDTO;
import com.bo.exception.RestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CallNumberService {


    @Value("${car.horn}")
    private String CAR_HORN;


    @Async("taskExecutor")
    public void CarHorn(String wagon, String produceLine) {
        try {
            log.info("车辆喇叭请求开始,车辆:{},生产线:{}", wagon, produceLine);
            //组成json数据
            CarHornDTO carHornDTO = new CarHornDTO();
            carHornDTO.setCar_no(wagon);
            if (produceLine.contains("1")) {
                carHornDTO.setLine_no("1号线");
            } else if (produceLine.contains("2")) {
                carHornDTO.setLine_no("2号线");
            }
            //发送请求
            String url = CAR_HORN;
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(carHornDTO));
            JSONObject post = RestUtil.post(url, jsonObject);
            String msg = post.getString("code");
            //String msg = "成功";
            if (msg.equals("200")) {
                log.info("车辆喇叭请求成功");
            }else {
                log.info("车辆喇叭请求失败");
            }
        }catch (Exception e){
            log.error("车辆喇叭请求失败,车辆:{},生产线:{}, 异常信息:{}", wagon, produceLine, e.getMessage(), e);
        }
    }

}
